﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WindowsFormsApplication3;
using System.Data.SqlClient;

namespace bsy
{
    public partial class ChildSeriesAdd : Form
    {
        public ChildSeriesAdd(string arg)
        {
            InitializeComponent();
            this.p_name = arg;
        }

        private string p_name="";

        public string P_name
        {
            get { return p_name; }
            set { p_name = value; }
        }


        private void ChildSeriesAdd_button1_Click(object sender, EventArgs e)
        {
            String childSeriesName = ChildSeriesAdd_textBox1.Text.Trim();
            String SeriesName = ChildSeriesAdd_textBox2.Text.Trim();
            if (childSeriesName == "" || SeriesName == "")
            {
                MessageBox.Show("输入值不能为空！","系统提示",MessageBoxButtons.OK,MessageBoxIcon.Warning);
                return;
            }
            else
            {
                try
                {
                    //检索数据库中是否存在重复的系列名称
                    String sql1 = string.Format("select PS_id from productsort where PS_name='{0}'", SeriesName);
                    DataSet ds1 = new DataSet();
                    ds1 = CommonDB.Query(sql1);
                    int PS_id1 = (int)ds1.Tables[0].Rows[0][0];

                    String sq2 = "select PS_name from productsort where P_id='"+PS_id1+"'";
                    DataSet ds = new DataSet();
                    ds = CommonDB.Query(sq2);
                    int count = ds.Tables[0].Rows.Count;//获取存入DataSet中Table的长度
                    for (int i = 0; i < count; i++)
                    {
                        String PS_name = (String)ds.Tables[0].Rows[i][0];
                        if (childSeriesName == PS_name)
                        {
                            MessageBox.Show("'" + childSeriesName + "'" + "已存在！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            return;
                        }
                    }
                    
                    String sql3 = "select count(*) from productsort where PS_name='" + SeriesName + "'";
                    DataSet dset = new DataSet();
                    dset = CommonDB.Query(sql3);
                    int cou = (int)dset.Tables[0].Rows[0][0];
                    if (cou == 0)
                    {
                        MessageBox.Show("所属产品系列不存在，请先在产品系列里添加！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    else
                    {
                        String sql2 = "select PS_id from productsort where PS_name='" + SeriesName + "'";
                        DataSet das = new DataSet();
                        das = CommonDB.Query(sql2);
                        int PS_id = (int)das.Tables[0].Rows[0][0];
                        String sql4 = "insert into productsort(PS_name,P_id) values('" + childSeriesName + "','"+PS_id+"')";
                        int temp = CommonDB.ExecuteSql(sql4);
                        if (temp == 1)
                        {
                            MessageBox.Show("添加成功！","系统提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
                            this.DialogResult = DialogResult.OK;
                        }
                        das.Dispose();
                    }
                    dset.Dispose();
                    ds.Dispose();
                    ds1.Dispose();
                    
                }
                catch (SqlException sqle)
                {
                    MessageBox.Show("错误提示："+sqle.Message);
                }
            }
        }
        private void ChildSeriesAdd_button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void ChildSeriesAdd_Load(object sender, EventArgs e)
        {
            ChildSeriesAdd_textBox2.Text = p_name;
        }
    }
}
